<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<fieldset [formGroup]="entityTypesVersionCreateFormGroup" class="fields-group flex flex-col">
  <legend class="group-title" translate>version-control.entities-to-export</legend>
  <div class="flex flex-col" style="flex: 1; overflow: auto;">
    @for (entityTypeFormGroup of entityTypesFormGroupArray(); track entityTypeFormGroup; let index = $index, isLast = $last) {
      <div class="flex flex-row items-center justify-start" [class.pb-2]="!isLast">
        <mat-expansion-panel class="entity-type-config flex-1" [class.load]="loading" [formGroup]="entityTypeFormGroup"
                             [expanded]="entityTypesFormGroupExpanded(entityTypeFormGroup)" (opened)="loading = false">
          <mat-expansion-panel-header>
            <div class="flex flex-1 flex-row items-center justify-start">
              <mat-panel-title>
                <div class="flex flex-1 flex-row items-center justify-start">
                  <div>{{ entityTypeText(entityTypeFormGroup) }}</div>
                </div>
              </mat-panel-title>
              <span class="flex-1"></span>
              <button [class.hidden]="disabled" mat-icon-button style="min-width: 40px;"
                      type="button"
                      (click)="removeEntityType(index)"
                      matTooltip="{{ 'action.remove' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>delete</mat-icon>
              </button>
            </div>
          </mat-expansion-panel-header>
          <div class="entity-type-config-content flex flex-col gap-2">
            <mat-divider></mat-divider>
            <div class="flex flex-row xs:flex-col gt-xs:gap-4">
              <tb-entity-type-select
                showLabel
                formControlName="entityType"
                appearance="outline"
                required
                [filterAllowedEntityTypes]="false"
                [allowedEntityTypes]="allowedEntityTypes(entityTypeFormGroup)">
              </tb-entity-type-select>
              <div class="flex flex-1 flex-row xs:flex-col gt-xs:gap-4" formGroupName="config">
                <mat-form-field class="mat-block flex-1" appearance="outline">
                  <mat-label translate>version-control.sync-strategy</mat-label>
                  <mat-select formControlName="syncStrategy">
                    <mat-option [value]="'default'">
                      {{ 'version-control.default' | translate }}
                    </mat-option>
                    @for (strategy of syncStrategies; track strategy) {
                      <mat-option [value]="strategy">
                        {{syncStrategyTranslations.get(strategy) | translate}}
                      </mat-option>
                    }
                  </mat-select>
                </mat-form-field>
                <div class="flex flex-1 flex-col">
                  <mat-checkbox [class.hidden]="entityTypeFormGroup.get('entityType').value !== entityTypes.DEVICE" formControlName="saveCredentials">
                    {{ 'version-control.export-credentials' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="saveAttributes">
                    {{ 'version-control.export-attributes' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="saveRelations">
                    {{ 'version-control.export-relations' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="!typesWithCalculatedFields.has(entityTypeFormGroup.get('entityType').value)" formControlName="saveCalculatedFields">
                    {{ 'version-control.export-calculated-fields' | translate }}
                  </mat-checkbox>
                </div>
              </div>
            </div>
            <div class="flex flex-row xs:flex-col gt-xs:min-h-19 gt-xs:items-center gt-xs:justify-start gt-xs:gap-4"
                 formGroupName="config">
              <mat-slide-toggle formControlName="allEntities">
                {{ 'version-control.all-entities' | translate }}
              </mat-slide-toggle>
              @if (!entityTypeFormGroup.get('config').get('allEntities').value) {
                <tb-entity-list
                  class="flex-1"
                  appearance="outline"
                  subscriptSizing="dynamic"
                  [entityType]="entityTypeFormGroup.get('entityType').value"
                  required
                  formControlName="entityIds">
                </tb-entity-list>
              }
            </div>
          </div>
        </mat-expansion-panel>
      </div>
    } @empty {
      <span translate class="tb-prompt flex items-center justify-center">version-control.no-entities-to-export-prompt</span>
    }
  </div>
  <div style="padding-top: 16px;" class="flex flex-row">
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!addEnabled()"
            (click)="addEntityType()">
      <span translate>version-control.add-entity-type</span>
    </button>
    <span class="flex-1"></span>
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!entityTypesFormGroupArray().length"
            (click)="removeAll()">
      <span translate>version-control.remove-all</span>
    </button>
  </div>
</fieldset>
